package com.dhl.rabbitmq.nine;

import com.dhl.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;

/**
 * @author danghailiang
 * 优先级队列 生产者
 */
public class PriorityProducer {
    public static void main(String[] args) throws Exception {
        // 获取信道
        Channel channel = RabbitUtils.getChannel();
        for (int i = 0; i < 255; i++) {
            String message = "info:===" + i;
            if("info:===200".equals(message)){
                // 设置消息优先级
                AMQP.BasicProperties build = new AMQP.BasicProperties().builder().priority(200).build();
                channel.basicPublish("priorityExchange","priorityKey",build,message.getBytes(StandardCharsets.UTF_8));

            }else{

                // 设置消息优先级
                int priorityNum= (int) (Math.random()*100);
                System.out.println(message + "==优先级为："+ priorityNum);
                AMQP.BasicProperties build = new AMQP.BasicProperties().builder().priority(priorityNum).build();
                channel.basicPublish("priorityExchange","priorityKey",build,message.getBytes(StandardCharsets.UTF_8));

            }
        }
    }
}
